﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Common
{
    public static class MdiController
    {

        private static readonly List<Form> _childForms;

        /// <summary>
        /// Add form to MDI Child node
        /// </summary>
        /// <typeparam name="T">Form type</typeparam>
        /// <history>
        /// 11/16/2014 phat.vo: Create new
        /// </history>
        public static void AddChildMdi<T>() where T : Form
        {
            // Open new form if there is no same form
            // otherwise, active form
            var type = typeof(T);
            var instance = (T)Activator.CreateInstance(type);
            var form = instance as Form;

            if (_childForms.Exists(x => x.Name == form.Name))
            {
                var childForm = _childForms.Find(x => x.Name == form.Name);
                childForm.Activate();
            }
            else
            {
                form.MdiParent = this;
                _childForms.Add(form);
                form.Show();
            }
        }
    }
}
